#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(std::shared_ptr<AppNode> appNode, QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);
    appNode_ = appNode;

    configCtl_ = new ConfigCtl(ui->configList, ui->configVal, ui->btnConfig, ui->freeConfigList, ui->freeConfigVal,
                               ui->btnFreeConfig, ui->btnConfigRefresh, ui->configFeedback, appNode, this);
    directionCtl_ = new DirectionCtl(ui->btnFront, ui->btnBack, ui->btnLeft, ui->btnRight, appNode, this);

    // 设置窗口接收键盘事件
    setFocusPolicy(Qt::StrongFocus);
}

MainWindow::~MainWindow() {
    delete configCtl_;
    delete directionCtl_;
    delete ui;
}

// 重写键盘事件处理函数
void MainWindow::keyPressEvent(QKeyEvent *event) {
    switch (event->key()) {
    case Qt::Key_Up:
        directionCtl_->btnFrontPressed(true);
        break;
    case Qt::Key_Down:
        directionCtl_->btnBackPressed(true);
        break;
    case Qt::Key_Left:
        directionCtl_->btnLeftPressed(true);
        break;
    case Qt::Key_Right:
        directionCtl_->btnRightPressed(true);
        break;
    default:
        break;
    }
}

void MainWindow::keyReleaseEvent(QKeyEvent *event) {
    switch (event->key()) {
    case Qt::Key_Up:
        directionCtl_->btnFrontPressed(false);
        break;
    case Qt::Key_Down:
        directionCtl_->btnBackPressed(false);
        break;
    case Qt::Key_Left:
        directionCtl_->btnLeftPressed(false);
        break;
    case Qt::Key_Right:
        directionCtl_->btnRightPressed(false);
        break;
    default:
        break;
    }
}

// void MainWindow::on_configVal_textEdited(const QString &arg1) {}
